package de.hft_stuttgart.dsa.ctt.app;

import java.util.ArrayList;
import java.util.List;

import de.hft_stuttgart.dsa.ctt.model.Course;
import de.hft_stuttgart.dsa.ctt.model.CourseAssignmentMatrix;
import de.hft_stuttgart.dsa.ctt.model.CourseCompetitionMatrix;
import de.hft_stuttgart.dsa.ctt.model.ProblemInstance;
import de.hft_stuttgart.dsa.ctt.processing.IAssigner;
import de.hft_stuttgart.dsa.ctt.processing.Preprocessor;
import de.hft_stuttgart.dsa.ctt.processing.TimeslotCourseAssigner;
import de.hft_stuttgart.dsa.ctt.reader.DatasetReader;
import de.hft_stuttgart.dsa.ctt.writer.SolutionWriter;

public class Main {

	// /**
	// * Main method for the project
	// */
	// public static void main(String[] args) {
	// ProblemInstance problemInstance = new
	// DatasetReader().readDataset(DatasetReader.DATASET1);
	// CourseAvailabilityMatrix matrix = new
	// CourseAvailabilityMatrix(problemInstance.getRooms(),
	// problemInstance.getPeriods());
	// Preprocessor preprocessor = new Preprocessor(problemInstance, matrix);
	// preprocessor.processCourseAvailabilityMatrix();
	// System.out.println(problemInstance.getCurricula());
	//
	// CourseCompetitionMatrix courseCompetitionMatrix = new
	// CourseCompetitionMatrix(problemInstance.getRooms(),
	// problemInstance.getPeriods(), problemInstance.getNumberOfCourses());
	// Preprocessor preprocessor2 = new Preprocessor(problemInstance,
	// courseCompetitionMatrix);
	// preprocessor2.processCourseCompetitionMatrix();
	//
	// // IAssigner assigner = new CourseTimeslotAssigner(problemInstance,
	// courseCompetitionMatrix);
	// // assigner.assignCoursesToMatrix();
	//
	// IAssigner timeslotCourseAssigner = new
	// TimeslotCourseAssigner(problemInstance, courseCompetitionMatrix);
	// timeslotCourseAssigner.assignCoursesToMatrix();
	//		
	// TimeTabler timeTabler = new TimeTabler();
	// timeTabler.create(800, 600);
	// }

	public static void main(String[] args) {
		List<ProblemInstance> problemInstances = new ArrayList<ProblemInstance>();
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET1));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET2));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET3));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET4));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET5));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET6));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET7));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET8));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET9));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET10));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET11));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET12));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET13));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET14));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET15));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET16));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET17));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET18));
//		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET19));
		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET20));
		problemInstances.add(new DatasetReader().readDataset(DatasetReader.DATASET21));
				
		for (ProblemInstance problemInstance : problemInstances) {
			// calculated the total number if lectures
			int numberOfLectures = 0;
			for (Course course : problemInstance.getAllCourses().values()) {
				numberOfLectures += course.getNumberOfLectures();
			}

			int assignedLectures = 0;
			int counter = 0;
			int periodOfFirstFound = 0;
			int iterations = 200;
			for (int i = 0; i < iterations; ++i) {
				CourseCompetitionMatrix courseCompetitionMatrix = new CourseCompetitionMatrix(problemInstance.getRooms(), problemInstance.getPeriods(), problemInstance.getNumberOfCourses());
				Preprocessor preprocessor = new Preprocessor(problemInstance, courseCompetitionMatrix);
				preprocessor.processCourseCompetitionMatrix();

				IAssigner timeslotCourseAssigner = new TimeslotCourseAssigner(problemInstance, courseCompetitionMatrix);
				CourseAssignmentMatrix matrix = timeslotCourseAssigner.assignCoursesToMatrix();
				assignedLectures = ((TimeslotCourseAssigner) timeslotCourseAssigner).getAssignmentCounter();
				if (null != matrix) {
					++counter;
					if (0 == periodOfFirstFound) {
						periodOfFirstFound = i + 1;
					}
					if (counter < 3) {
						SolutionWriter writer = new SolutionWriter();
						writer.writeSolutionToFile(problemInstance, matrix);
					}
//					System.out.println("# of lectures: " + numberOfLectures);
//					System.out.println("# of assigned lectures: " + assignedLectures);
				}
			}
			System.out.println(problemInstance.getName() + ": " + counter + " of " + iterations + " feasible solutions found. First found in iteration: " + periodOfFirstFound);
		}

	}

}
